//
//  ALShopsViewController.m
//  Ummy.G
//
//  Created by 安林 on 16/9/25.
//  Copyright © 2016年 安林. All rights reserved.
//

#import "ALShopsViewController.h"
#import "AppDelegate.h"
#import "ALDrop.h"
#import "ALMapController.h"
#import "SearchDisPlayerController.h"
#import "ALShopsTableViewCell.h"

#import "ALThirdTableViewCell.h"
#import "ALShopDown.h"

#import "ALAllShopsController.h"
#import "ALSaleShopsController.h"


@interface ALShopsViewController ()<UIScrollViewDelegate>

// 添加内容的scrollerView
@property(nonatomic,strong)UIScrollView *scrollerView;

//声明 tableView
@property(nonatomic,strong)UITableView *tableView;



//第一个蒙版上的tableVIEW
@property(nonatomic,strong)ALDrop *tableView11;
//蒙版
@property(nonatomic,strong)UIView *viewGray11;


//第二个蒙版上的tableVIEW
@property(nonatomic,strong)ALDrop *tableView22;
//蒙版
@property(nonatomic,strong)UIView *viewGray22;


//第三个蒙版上的tableVIEW
@property(nonatomic,strong)ALDrop *tableView33;
//蒙版
@property(nonatomic,strong)UIView *viewGray33;



//"商家"界面那三个
@property (nonatomic,strong) UIButton * choseBtn;

//第一个下拉菜单
@property(nonatomic,weak)UIBarButtonItem *chose1Item;
//第二个下拉菜单
@property(nonatomic,weak)UIBarButtonItem *chose2Item;
//第三个下拉菜单
@property(nonatomic,weak)UIBarButtonItem *chose3Item;

//@property(nonatomic,strong)UIPopoverController *popover;

@end

@implementation ALShopsViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBarHidden =YES;
    
    [self settingNavigation];
    
    [self initViews];
    
    [self addScrollerView];
    
    [self addChildController];
    
    [self showViewController:0];
    
}

#pragma mark 加载那两个子控制器
- (void)addChildController{

    [self addChildViewController:[[ALAllShopsController alloc]init]];
    
     [self addChildViewController:[[ALSaleShopsController alloc]init]];
}

-(void)showViewController:(NSInteger)index{
    UIViewController * vc = self.childViewControllers[index];
    
    // 实现视图的懒加载
    if (vc.isViewLoaded) {
        return;
    }
    
    CGFloat offSetX = index * UIScreenW;
    
    vc.view.frame = CGRectMake(offSetX, 0, self.scrollerView.frame.size.width, self.scrollerView.frame.size.height);
    
    [self.scrollerView addSubview:vc.view];
    
}



#pragma mark 添加上面的ScrollerView
- (void)addScrollerView{

    _scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 104, UIScreenW, UIScreenH-40)];
    
    self.scrollerView.contentSize = CGSizeMake(self.childViewControllers.count*UIScreenW, 0);
    
    self.scrollerView.bounces = NO;
    self.scrollerView.pagingEnabled = YES;
    self.scrollerView.showsHorizontalScrollIndicator = NO;
//    self.scrollerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.scrollerView];
}


- (void)settingNavigation{
    
    //《商家》顶部建一个大的view
    UIView *NavView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreenW, 64)];
    NavView.backgroundColor=ALColor(250, 250, 250);
    [self.view addSubview:NavView];
    
    
    //创建“地图”
    UIButton *map = [UIButton buttonWithType:UIButtonTypeCustom];
    map.frame = CGRectMake(2, 25, 45, 35);
    [map setImage:[UIImage imageNamed:@"icon_homepage_map"] forState:UIControlStateNormal];
    [map addTarget:self action:@selector(mapclick) forControlEvents:UIControlEventTouchUpInside];
    [NavView addSubview:map];
    
    
    //创建“搜索”
    UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom];
    search.frame = CGRectMake(UIScreenW-42, 25, 45, 35);
    [search setImage:[UIImage imageNamed:@"icon_navigationItem_search"] forState:UIControlStateNormal];
    [search addTarget:self action:@selector(searchclick) forControlEvents:UIControlEventTouchUpInside];
    [NavView addSubview:search];
    
    
    //"全部商家"
    UIButton *segment1=[UIButton buttonWithType:UIButtonTypeCustom];
    segment1.frame=CGRectMake(UIScreenW/2-80, 30, 80, 30);
    segment1.tag=20;
    [segment1 setTitle:@"全部商家" forState:UIControlStateNormal];
    [segment1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [segment1 setTitleColor:navigationBarColor forState:UIControlStateNormal];
    [segment1 setBackgroundColor:navigationBarColor];
    segment1.selected=YES;
    segment1.font= [UIFont systemFontOfSize:15];
    segment1.layer.borderWidth=1;
    segment1.layer.borderColor=[navigationBarColor CGColor];
    [segment1 addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventTouchUpInside];
    [NavView addSubview:segment1];
    
    
    //“优惠商家”
    UIButton *segment2 = [UIButton buttonWithType:UIButtonTypeCustom];
    segment2.frame = CGRectMake(UIScreenW/2, 30, 80, 30);
    segment2.tag = 21;
    [segment2 setTitle:@"优惠商家" forState:UIControlStateNormal];
    [segment2 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [segment2 setTitleColor:navigationBarColor forState:UIControlStateNormal];
    [segment2 setBackgroundColor:[UIColor whiteColor]];
    segment2. font = [UIFont systemFontOfSize:15];
    segment2.layer.borderWidth = 1;
    segment2.layer.borderColor = [navigationBarColor CGColor];
    [segment2 addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventTouchUpInside];
    [NavView addSubview:segment2];
    

}




#pragma mark “全部商家”“优惠商家”按钮单击事件的实现
- (void)clickSegment:(UIButton *)sender{
    
    
    NSInteger tag = sender.tag;
    UIButton *segment1= (UIButton *)[self.view viewWithTag:20];
    UIButton *segment2= (UIButton *)[self.view viewWithTag:21];
    [segment1 setBackgroundColor:[UIColor whiteColor]];
    [segment2 setBackgroundColor:[UIColor whiteColor]];
    segment1.selected=NO;
    segment2.selected=NO;
    sender.selected=YES;
    [sender setBackgroundColor:navigationBarColor];
    if (tag==20) { //全部商家
        self.scrollerView.contentOffset = CGPointMake(0, 104);
        
        [self showViewController:0];
        
    }else{ //优惠商家
        self.scrollerView.contentOffset = CGPointMake(UIScreenW, 104);
        
        [self showViewController:1];
        
    }
    
    
}


#pragma mark 那三个分类·
- (void)initViews{

    //选择
    UIView *choseView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, UIScreenW, 40)];
    choseView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:choseView];
    
    NSArray *choseName=@[@"全部分类",@"全城",@"离我最近"];
    for (int i=0; i<3; i++) {
        //上面的文字
        self.choseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.choseBtn.frame=CGRectMake(i*UIScreenW/3, 0, UIScreenW/3, 40);
        self.choseBtn.backgroundColor = [UIColor clearColor];
        self.choseBtn.tag=i;
        self.choseBtn.font=[UIFont systemFontOfSize:13];
        [self.choseBtn setTitle:choseName[i] forState:UIControlStateNormal];
        [self.choseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.choseBtn setTitleColor:navigationBarColor forState:UIControlStateSelected];
        if (i == 0) {
            [self.choseBtn addTarget:self action:@selector(ChoseBtn1:) forControlEvents:UIControlEventTouchUpInside];

        }else if (i == 1) {
            [self.choseBtn addTarget:self action:@selector(ChoseBtn2) forControlEvents:UIControlEventTouchUpInside];

        }else {
            [self.choseBtn addTarget:self action:@selector(ChoseBtn3) forControlEvents:UIControlEventTouchUpInside];

        }
        [choseView addSubview:self.choseBtn];
        
        //文字旁边的三角
        UIButton *triangle = [UIButton buttonWithType:UIButtonTypeCustom];
        triangle.frame = CGRectMake((i+1)*UIScreenW/3-15, 20, 8, 7);
        triangle.tag = 120+i;
        [triangle setImage:[UIImage imageNamed:@"icon_ordercenter_arrow_down"] forState:UIControlStateNormal];
        [triangle setImage:[UIImage imageNamed:@"icon_ordercenter_arrow_up"] forState:UIControlStateSelected];
        [choseView addSubview:triangle];
        
        //下划线
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 39.5, UIScreenW, 0.5)];
        line.backgroundColor=ALColor(192, 192, 192);
        [choseView addSubview:line];
        

    }
    
}


#pragma mark 地图单击事件的实现
- (void)mapclick{
    
    ALMapController *map = [[ALMapController alloc]init];
    [self.navigationController pushViewController:map animated:YES];
}


#pragma mark 搜索按钮单击事件的实现
- (void)searchclick{
 
    SearchDisPlayerController *search = [[SearchDisPlayerController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
}


- (void)setUpTableView{
    //添加下拉刷新的动画图片
    //[self.tableView ]
    
}

- (void)ChoseBtn1:(UIButton *)sender{
    
//    sender.selected = !sender.selected;
 
   //显示分类菜单
//
    _tableView11 = [ALDrop drop];
    _tableView11.frame = CGRectMake(0, 104, UIScreenW, UIScreenH*0.5);
   
//----------------创建蒙版---------------
    
    //创建灰色背景
     _viewGray11= [[UIView alloc]initWithFrame:CGRectMake(0, 104, UIScreenW, UIScreenH)];
    _viewGray11.alpha =0.3;
     _viewGray11.backgroundColor = [UIColor blackColor];
     [self.view addSubview:_viewGray11];
    
    //添加手势事件，移除view
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView:)];
    [_viewGray11 addGestureRecognizer:tap];
    
    [self.view addSubview:_tableView11];
}

- (void)dismissView:(UITapGestureRecognizer *)tap{
    [self dismissView];
}


//手势点击事件，移除view
-(void)dismissView{
    
   [UIView animateWithDuration:0.5 animations:^{
       
       [_viewGray11 removeFromSuperview];
       [_viewGray22 removeFromSuperview];
       [_viewGray33 removeFromSuperview];
   } completion:^(BOOL finished) {
       [_tableView11 removeFromSuperview];
       [_tableView22 removeFromSuperview];
       [_tableView33 removeFromSuperview];

       
   }];
}

- (void)ChoseBtn2{
    
    //显示分类菜单
    _tableView22 = [ALDrop drop];
    _tableView22.frame = CGRectMake(0, 104, UIScreenW, UIScreenH*0.5);
    
    //----------------创建蒙版---------------
    
    //创建灰色背景
    _viewGray22= [[UIView alloc]initWithFrame:CGRectMake(0, 104, UIScreenW, UIScreenH)];
    _viewGray22.alpha =0.3;
    _viewGray22.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_viewGray22];
    
    //添加手势事件，移除view
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView:)];
    [_viewGray22 addGestureRecognizer:tap];
    
    [self.view addSubview:_tableView22];
    
}

- (void)ChoseBtn3{
    
    //显示分类菜单
    _tableView33 = [ALDrop drop];
    _tableView33.frame = CGRectMake(0, 104, UIScreenW, UIScreenH*0.5);
    
    //----------------创建蒙版---------------
    
    //创建灰色背景
    _viewGray33= [[UIView alloc]initWithFrame:CGRectMake(0, 104, UIScreenW, UIScreenH)];
    _viewGray33.alpha =0.3;
    _viewGray33.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_viewGray33];
    
    //添加手势事件，移除view
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView:)];
    [_viewGray33 addGestureRecognizer:tap];
    
    [self.view addSubview:_tableView33];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.meituan.com/deal/28263284"]];
        NSLog(@"34323");
    }
}
 
@end

